//
//  MyMovieCollectionViewController.m
//  12.30_homework_豆瓣电影MVC
//
//  Created by dllo on 16/1/8.
//  Copyright © 2016年 lanou3g. All rights reserved.
//

#import "MyMovieCollectionViewController.h"
#import "LXC_DataBaseHandle.h"
#import "MovieModel.h"


@interface MyMovieCollectionViewController () <UITableViewDelegate, UITableViewDataSource>

@property (nonatomic, retain) UITableView *tableView;

@property (nonatomic, retain) NSMutableArray *arrData;

@end

@implementation MyMovieCollectionViewController

- (void)dealloc {
    [super dealloc];
}
- (instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        
    }
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.navigationItem.title = @"我的电影";
    self.navigationController.navigationBar.translucent = NO;
    self.view.backgroundColor = [UIColor whiteColor];
    
    self.tableView = [[UITableView alloc] initWithFrame:self.view.frame style:(UITableViewStylePlain)];
    [self.view addSubview:_tableView];
    [_tableView release];
    
    _tableView.delegate = self;
    _tableView.dataSource = self;
    
    [self.tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"cell"];
    
    self.arrData = [[LXC_DataBaseHandle shareDataBaseHandle] selectModelFromMovie];
    
    // 设置右的Button进入编辑模式
    self.navigationItem.rightBarButtonItem = self.editButtonItem;
    
}

// 重写系统的编辑按钮的触发方法
- (void)setEditing:(BOOL)editing animated:(BOOL)animated {
    [super setEditing:editing animated:animated];
    // 设置tableView的编辑方法
    [_tableView setEditing:editing animated:animated];
}

// 实现编辑功能
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
    
    MovieModel *model = [MovieModel movie];
    model = _arrData[indexPath.row];
 
    // 首先从数组里删掉 然后从tabView移除 再从数据库中删掉
    [_arrData removeObjectAtIndex:indexPath.row];
    [[LXC_DataBaseHandle shareDataBaseHandle] deleteModel:model.title tabName:@"MyMovieCollection"];
    // 如果不从数组里移除的 下次返回行数的时候就会数组越界,崩溃
    [_tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationLeft];
    
}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return _arrData.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
    
    MovieModel *model = _arrData[indexPath.row];
    
    cell.textLabel.text = model.title;
    
    return cell;
    
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    
    
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
